package com.servlet;

import com.dao.Impl.NewsDaoImpl;
import com.dao.Impl.StaffDaoImpl;
import com.entity.News;
import com.entity.PageBean;
import com.entity.Staff;
import com.util.JDBC;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/newsController/*")
public class NewsManageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methon = MyUtil.getMethon(request);
        switch (methon) {
            case "show":
                selectNews(request, response);
                break;
            case "findStaffIdByName":
                findStaffIdByName(request, response);
                break;
            case "selectId":
                selectId(request, response);
                break;
            case "update":
                update(request, response);
                break;
            case "delete":
                delete(request, response);
                break;
            case "add":
                add(request, response);
                break;
            default:
                request.getRequestDispatcher("/newsManage/announcement.jsp").forward(request, response);
                break;
        }
    }

    /**
     *
     * @param request
     * @param response
     */
    private void add(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
//        Staff staff = (Staff) request.getSession().getAttribute("staff");
        News news = new News();
        TransBean.populate(news,map);
//        news.setStaffId(staff.getStaffId());
        news.setStaffId(20190001);
        int insert = new NewsDaoImpl().insert(news);
        if(insert>0){
            MyUtil.jsonWriter(response,"1");
        }else{
            MyUtil.jsonWriter(response,"0");
        }
    }

    /**
     *
     * @param request
     * @param response
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        int id = new NewsDaoImpl().detele(Integer.parseInt(request.getParameter("id")));
        MyUtil.jsonWriter(response, id);
    }

    /**
     *
     * @param request
     * @param response
     */
    private void update(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        News news = new News();
        TransBean.populate(news,map);
        int update = new NewsDaoImpl().update(news);
        if(update>0){
            MyUtil.jsonWriter(response,"1");
        }else{
            MyUtil.jsonWriter(response,"0");
        }
    }

    /**
     *
     * @param request
     * @param response
     */
    private void selectId(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        News news = new NewsDaoImpl().findById(id);
        Staff staff = new StaffDaoImpl().findStaffId(news.getStaffId());
        Map map = new HashMap();
        map.put("news",news);
        map.put("staffName",staff.getStaffName());
        MyUtil.jsonWriter(response,map);
    }

    /***
     *
     * @param request
     * @param response
     */
    private void findStaffIdByName(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        List<Staff> staffs = new StaffDaoImpl().findStaffId(name);
        MyUtil.jsonWriter(response,staffs);
    }

    /**
     *
     * @param request
     * @param response
     */
    private void selectNews(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        TransBean.populate(pageBean, request.getParameterMap());
        Map map = new HashMap<>();

        Staff staff = (Staff) request.getSession().getAttribute("staff");
        map.put("staff", staff);
        List<Staff> staffs = new StaffDaoImpl().findAll();
        map.put("staffs", staffs);

        new NewsDaoImpl().findAllNews(pageBean);
        map.put("page", pageBean);

        map.put("staff", request.getSession().getAttribute("staff"));

        MyUtil.jsonWriter(response,map);
    }

}
